﻿using KinonekoSoftware.JuXiaoYouDB.Core;
using KinonekoSoftware.JuXiaoYouDB.Documents.General.Properties;
using KinonekoSoftware.JuXiaoYouDB.Objects;
using KinonekoSoftware.JuXiaoYouDB.SubSystem;
using InstanceType = KinonekoSoftware.JuXiaoYouDB.SubSystem.Categories.InstanceType;

namespace KinonekoSoftware.Studio.Galleries
{
    public sealed partial class InstanceGalleryViewModel : TabViewModelCore
    {
        //-------------------------------------------------------------
        //
        //          ReadOnly Fields
        //
        //-------------------------------------------------------------
        
        //-------------------------------------------------------------
        //
        //          Fields
        //
        //-------------------------------------------------------------
        private InstanceType _type;
        private GlossaryDB   _GlossaryDB;
        private Glossary     _selected;

        
        //-------------------------------------------------------------
        //
        //          Constructors
        //
        //-------------------------------------------------------------
        public InstanceGalleryViewModel()
        {
            _GlossaryDB = GetDatabase();
            
            
            Containers = new ObservableList<PropertyContainer>
            {
                new PropertyContainer
                {
                    Name = "Container #1",
                    Elements = new ObservableList<PropertyElement>
                    {
                        new StringElement{ Name = "Field #1", },
                        new StringElement{ Name = "Field #2", },
                        new StringElement{ Name = "Field #3", },
                        new StringElement{ Name = "Field #4", },
                    },
                },
                
                new PropertyContainer
                {
                    Name = "Container #2",
                    Elements = new ObservableList<PropertyElement>
                    {
                        new StringElement{ Name = "Field #1", },
                        new StringElement{ Name = "Field #2", },
                        new StringElement{ Name = "Field #3", },
                        new StringElement{ Name = "Field #4", },
                        new StringElement{ Name = "Field #5", },
                        new StringElement{ Name = "Field #6", },
                        new StringElement{ Name = "Field #7", },
                        new StringElement{ Name = "Field #8", },
                    },
                },
                
                
                new PropertyContainer
                {
                    Name = "Container #3",
                    Elements = new ObservableList<PropertyElement>
                    {
                        new StringElement{ Name = "Field #1", },
                        new StringElement{ Name = "Field #2", },
                        new StringElement{ Name = "Field #3", },
                        new StringElement{ Name = "Field #4", },
                        new StringElement{ Name = "Field #5", },
                        new StringElement{ Name = "Field #6", },
                    },
                },
            };
            
            InstanceTypes               = new ObservableList<InstanceType>();
            GlossaryCollection          = new ObservableCollection<Glossary>();
            Filters                     = new ObservableCollection<GalleryFilter>();
            
            ShiftToSelectionModeCommand       = CreateCommand(DoShiftToSelectionModeCommand);
            ShiftToSettingEditModeCommand     = CreateCommand(DoShiftToSettingEditModeCommand);
            ShiftToGroupEditModeCommand       = CreateCommand(DoShiftToGroupEditModeCommand);
            ShiftToStoryEditModeCommand       = CreateCommand(DoShiftToStoryEditModeCommand);
            ShiftToInspirationEditModeCommand = CreateCommand(DoShiftToInspirationEditModeCommand);
            
            AddGlossaryCommand          = CreateCommand(DoAddGlossaryCommand);
            UpdateGlossaryCommand       = CreateCommand<Glossary>(DoUpdateGlossaryCommand);
            RemoveGlossaryCommand       = CreateCommand<Glossary>(DoRemoveGlossaryCommand);

            IsSingleton = true;
            Removable   = false;
        }

        //-------------------------------------------------------------
        //
        //          Private Methods
        //
        //-------------------------------------------------------------

        private static GlossaryDB GetDatabase()
        {
            //
            // 这是一个懒加载模式，也就是说打开一个文件的操作不应该放在主逻辑中。
            if (GlossaryDB.IsCreated)
            {
                return GlossaryDB.Instance;
            }
            
            Platform.Collect(GlossaryDB.Instance);
            return GlossaryDB.Instance;
        }

        
        //-------------------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------------------
        protected override async void OnStart()
        {
            using (var session = await Ioc.Get<IBusyServiceEX>().BeginSession(""))
            {
                //
                // 首先确保GlossaryDB创建了，并且首次创建时应该加入DisposableCollection，由该程序托管生命周期
                if (GlossaryDB.IsCreated)
                {
                    _GlossaryDB = GlossaryDB.Instance;
                }
                else
                {
                    
                    Platform.Collect(GlossaryDB.Instance);
                    _GlossaryDB = GlossaryDB.Instance;
                }
                
                //
                // 第二步，加载数据拦截器
                
                //
                //
                await session.Await(2);
            }

            //
            // 初始化编辑器
            Mode = InstanceGalleryMode.SelectionMode;
        }
        

        //-------------------------------------------------------------
        //
        //          Public Methods
        //
        //-------------------------------------------------------------
        
        
        //-------------------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------------------
        public ObservableList<InstanceType>      InstanceTypes      { get; }
        public ObservableList<PropertyContainer> Containers         { get; }
        public ObservableCollection<Glossary>    GlossaryCollection { get; }

        
        /// <summary>
        /// 获取或设置 <see cref="Selected"/> 属性。
        /// </summary>
        public Glossary Selected
        {
            get => _selected;
            set
            {
                SetValue(ref _selected, value);
                WhenSelectedChangedUpdatePropertyBridge();
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="Type"/> 属性。
        /// </summary>
        public InstanceType Type
        {
            get => _type;
            set => SetValue(ref _type, value);
        }
    }
}